/**
 * 
 */
package com.jm.web.builder;

import java.io.IOException;

import com.jm.utils.AttributeBuilder;
import com.jm.utils.StringHelper;
import com.jm.web.form.Fieldset;
import com.jm.web.form.Group;
import com.jm.web.tags.support.BaseTag;

/**
 * @author yanjie
 *
 */
public class FieldsetBuilder extends BaseBuilder{
	protected Fieldset fieldset = null ;
	
	@Override
	public void init(BaseTag tag) {
		if(this.fieldset == null){
			super.init(tag);
			fieldset = new Fieldset();
			fieldset.InitTag(tag);
			fieldset.setNodeName("fieldset");
			fieldset.setFromTag(true);//设置此控件是tag生成的
		}
	}
	
	@Override
	public void RenderStart() {
		try {
			this.getOut().write(StringHelper.Format("<%1$s ",fieldset.getNodeName()));
			AttributeBuilder attributesBuilder = new AttributeBuilder();
			attributesBuilder.InitFromHashtable(this.getBaseTag().getExtAttrList(),true);
			fieldset.FillAttributeBuilder(attributesBuilder);
			this.getOut().write(attributesBuilder.ToOutputString());
			this.getOut().write(">");
			
			if(this.getBaseTag().GetExtValue("keep",false)){
				this.getOut().write("<keep>");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void RenderEnd() {
		if(!StringHelper.IsNullOrEmpty(fieldset.getNodeName())){
			try {
				if(this.getBaseTag().GetExtValue("keep",false)){
					this.getOut().write("</keep>");
				}
				this.getOut().write(StringHelper.Format("</%1$s>",fieldset.getNodeName()));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
